package org.sysdance.aplicativo.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

import org.sysdance.aplicativo.excecao.RelatorioException;

public final class RelatorioUtil {
	
	private RelatorioUtil() {
		super();
	}
	
	public static void gerarRelatorio(final String nomeRelatorio, final Collection<?> dadosRelatorio,final Map<String, Object> parametros)throws RelatorioException{
		try {
			final InputStream arquivoRelatorio = new FileInputStream(nomeRelatorio);
			final JasperDesign relatorioJRXML = JRXmlLoader.load(arquivoRelatorio);
			final JasperReport relatorioCompilado = JasperCompileManager.compileReport(relatorioJRXML);
			final JasperPrint relatorioImpressao = JasperFillManager.fillReport(relatorioCompilado, parametros, new JRBeanCollectionDataSource(dadosRelatorio));
			JasperViewer.viewReport(relatorioImpressao);
		} catch (Exception e) {
			throw new RelatorioException(e.getLocalizedMessage());
		}
	}
	
}
